/*********************************************************************
 *
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2008, 2013, Willow Garage, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 * Author: Eitan Marder-Eppstein
 *         David V. Lu!!
 *********************************************************************/
#ifndef COSTMAP_2D_COSTMAP_LAYER_H_
#define COSTMAP_2D_COSTMAP_LAYER_H_
#include <ros/ros.h>
#include <costmap_2d/layer.h>
#include <costmap_2d/layered_costmap.h>

namespace costmap_2d{

class CostmapLayer : public Layer, public Costmap2D{
public:
  CostmapLayer() : has_extra_bounds_(false), extra_min_x_(1e6), extra_max_x_(-1e6), extra_min_y_(1e6), extra_max_y_(-1e6) {}
  // Discretized 离散化的
  bool isDiscretized() { return true; }
  virtual void matchSize();
  virtual void clearArea(int start_x, int start_y, int end_x, int end_y, bool invert_area=false);

  // 如果一个外部源改变了costmap中的值，它应该用它改变的区域调用这个方法，以确保costmap也包括这个区域
  void addExtraBounds(double mx0, double my0, double mx1, double my1);

protected:
  // 使用此图层的值更新指定边界框中的 master_grid, TrueOverwrite 意味着该层的每个值都被写入主网格
  void updateWithTrueOverwrite(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j);
  // 使用此图层的值更新指定边界框中的 master_grid，Overwrite意味着来自该层的每个有效值都被写入主网格(不复制 NO_INFORMATION)
  void updateWithOverwrite(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j);
  // 使用此图层的值更新指定边界框中的 master_grid。 将新值设置为 master_grid 的值和该层的值的最大值。
  // 如果主值为 NO_INFORMATION，则将其覆盖。 如果图层的值为 NO_INFORMATION，则主值不会更改。
  void updateWithMax(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j);
  // 使用此图层的值更新指定边界框中的 master_grid。
  // 将新值设置为主格网值与该层值之和。 如果主值是 NO_INFORMATION，它会被图层的值覆盖。 如果图层的值为 NO_INFORMATION，则主值不会更改。
  // 如果总和值大于 INSCRIBED_INFLATED_OBSTACLE，则主值设置为 (INSCRIBED_INFLATED_OBSTACLE - 1)。
  void updateWithAddition(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j);
  // 更新参数中指定的边界框以包含位置(x,y)
  void touch(double x, double y, double* min_x, double* min_y, double* max_x, double* max_y);
  // 更新参数中指定的边界框以包括来自 addExtraBounds 调用的边界框。 如果未调用 addExtraBounds，则该方法将不执行任何操作。 应该在 updateBounds 方法的开头调用
  void useExtraBounds(double* min_x, double* min_y, double* max_x, double* max_y);
  bool has_extra_bounds_;

private:
  double extra_min_x_, extra_max_x_, extra_min_y_, extra_max_y_;
};

}  // namespace costmap_2d
#endif  // COSTMAP_2D_COSTMAP_LAYER_H_
